package com.woniu.tms.attendance.controller;


import com.fasterxml.jackson.databind.JavaType;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.tms.attendance.service.AttendanceService;
import com.woniu.tms.attendance.service.SickFormService;
import com.woniu.tms.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.*;

/**
 * @Author: peng
 */
@Controller
@RequestMapping("attendance")
public class AttendanceController {
    private static ObjectMapper MAPPER = new ObjectMapper();
    @Autowired
    AttendanceService attendanceService;
    @Autowired
    SickFormService sickFormService;

    /**
     * 学生跳转考勤信息展示页面
     *
     * @return
     */
    @RequestMapping("studentIndex")
    public String studentIndex() {

        return "WEB-INF/attendance/studentIndex";
    }

    /**
     * 展示学生本人考勤信息
     *
     * @param uid
     * @param examplesMap
     * @param pageNum
     * @return
     * @throws IOException
     */
    @RequestMapping("listSelf")
    @ResponseBody
    public Map<String, Object> listSelf(Integer uid, String examplesMap, @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) throws IOException {
        PageHelper.startPage(pageNum, pageSize);
        Map<String, Object> map = new HashMap<>();
        CheckinfoExample example = new CheckinfoExample();
        CheckinfoExample.Criteria criteria = example.createCriteria();
        criteria.andStudentidEqualTo(uid);
        /**
         * 将前台传递的json字符传转换为map集合
         */
        if (examplesMap != null && !examplesMap.equals("")) {
            JavaType jvt = MAPPER.getTypeFactory().constructParametricType(HashMap.class, String.class, String.class);
            Map<String, String> urMap = MAPPER.readValue(examplesMap, jvt);

            if (urMap != null && urMap.size() != 0) {

                /**
                 * 起始日期minDate
                 */
                if (urMap.get("minDate") != null && !urMap.get("minDate").equals("")) {
                    criteria.andTimesGreaterThanOrEqualTo(new Date(urMap.get("minDate")));
                }
                /**
                 * examples[1]为结束日期maxDate
                 */
                if (urMap.get("maxDate") != null && !urMap.get("maxDate").equals("")) {
                    criteria.andTimesLessThanOrEqualTo(new Date(urMap.get("maxDate")));
                }
                /**
                 * examples[2]为考勤状态cfstatus
                 */
                if (urMap.get("cfstatus") != null && !urMap.get("cfstatus").equals("")) {
                    criteria.andCfstatusEqualTo(urMap.get("cfstatus"));
                }
            }
        }

        try {

            List<Checkinfo> checkinfos = attendanceService.listSelf(example);

            PageInfo<Checkinfo> pageInfo = new PageInfo<>(checkinfos);
            map.put("result", true);
            map.put("checkinfos", checkinfos);
            map.put("pageInfo", pageInfo);

        } catch (Exception e) {
            map.put("result", false);
            e.printStackTrace();
        } finally {
            return map;
        }
    }

    /**
     * 讲师跳转本班学生的考勤信息
     *
     * @return
     */
    @RequestMapping("teacherIndex")
    public String teacherIndex() {

        return "WEB-INF/attendance/teacherIndex";
    }

    /**
     * 展示讲师所属班级学生的考勤信息
     * 展示班主任所属班级学生的考勤信息
     *
     * @return
     */
    @RequestMapping("listByTid")
    @ResponseBody
    public Map<String, Object> listByTid(Integer uid, String examplesMap, @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {

        Map<String, Object> map = new HashMap<>();


        try {
            Map<String, Object> result = attendanceService.listSelfClassByTid(uid, examplesMap, pageNum, pageSize);

            List<Sickform> sickforms = attendanceService.listSickInfo(uid);
            Integer size = sickforms.size();
            List<Classes> classesList = attendanceService.listClassesByTidOrCmid(uid);
            map.put("result", true);//操作成功与否
            map.put("classesid", result.get("classesid"));//放入班级id
            map.put("classesList", classesList);//放入班级列表
            map.put("checkinfos", result.get("checkinfoList"));//放入考勤信息列表
            map.put("size", size);//放入需审核考勤细细条数
            map.put("pageInfo", result.get("pageInfo"));//放入分页信息
        } catch (Exception e) {
            map.put("result", false);
            e.printStackTrace();
        } finally {
            return map;
        }
    }

    /**
     * 班主任跳转，其对应班级学生页面
     *
     * @return
     */
    @RequestMapping("classmanagerIndex")
    public String classmanagerIndex() {
        return "WEB-INF/attendance/classmanagerIndex";
    }

    /**
     * 填充修改模态框数据
     *
     * @param cfid
     * @return
     */
    @RequestMapping("update")
    @ResponseBody
    public Map<String, Object> update(Integer cfid) {
        Map<String, Object> map = new HashMap<>();
        try {
            Checkinfo checkinfo = attendanceService.selectByCfid(cfid);
            map.put("checkinfo", checkinfo);
            map.put("result", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result", false);
        }
        return map;
    }


    /**
     * 班主任修改学员考勤信息
     *
     * @param checkinfo
     * @return
     */
    @RequestMapping("confirmUpdate")
    @ResponseBody
    public Map<String, Object> confirmUpdate(Checkinfo checkinfo) {
        Map<String, Object> map = new HashMap<>();
        try {

            attendanceService.confirmUpdate(checkinfo);
            map.put("result", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result", false);
        }
        return map;
    }

    /**
     * 进入添加考勤信息页面
     *
     * @param checkinfo
     * @return
     */
    @RequestMapping("addSome")
    public String addSome(Checkinfo checkinfo) {

        return "WEB-INF/attendance/classmanagerAdd";
    }

    /**
     * 班主任查询其所对应班级的全部学生
     *
     * @return
     */
    @RequestMapping("listStudent")
    @ResponseBody
    public Map<String, Object> listStudent(Integer uid, String examplesMap, @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        try {
            Map<String, Object> result = attendanceService.listStudent(uid, examplesMap, pageNum, pageSize);
            List<Classes> classesList = attendanceService.listClassesByTidOrCmid(uid);

            map.put("pageInfo", result.get("pageInfo"));
            map.put("studentList", result.get("studentList"));
            map.put("classesList", classesList);
            map.put("result", true);
            map.put("classesid", result.get("classesid"));
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result", false);
        } finally {
            return map;
        }
    }

    /**
     * 班主任批量增加学员考勤信息
     *
     * @return
     */
    @RequestMapping("addSomeDo")
    @ResponseBody
    public Map<String, Object> addSomeDo(String times, String[] studentids, String[] cfstatus) {
        Map<String, Object> map = new HashMap<>();
        List<Checkinfo> checkinfoList = new ArrayList<>();

        Map<String, Object> parameterMap = new HashMap<>();

        /**
         * 组合实体类
         */
        for (int i = 0; i < studentids.length; i++) {
            Checkinfo checkinfo = new Checkinfo();
            checkinfo.setTimes(new Date(times));
            checkinfo.setStudentid(Integer.parseInt(studentids[i]));
            checkinfo.setCfstatus(cfstatus[i]);
            checkinfoList.add(checkinfo);
        }
        try {
            parameterMap.put("checkinfoList", checkinfoList);
            attendanceService.addSomeCheckinfo(parameterMap);
            map.put("result", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result", false);
        } finally {
            return map;
        }
    }


    /**
     * 班主任批量增加学员考勤信息
     *
     * @return
     */
    @RequestMapping("delSomeDo")
    @ResponseBody
    public Map<String, Object> delSome(@RequestParam(value = "cfids[]") Integer[] cfids) {
        Map<String, Object> map = new HashMap<>();


        try {
            attendanceService.delSome(cfids);
            map.put("result", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result", false);
        } finally {
            return map;
        }
    }

    /**
     * 就业专员进入其首页
     *
     * @return
     */
    @RequestMapping("empIndex")
    public String empIndex() {
        return "WEB-INF/attendance/empIndex";
    }

    /**
     * 就业专员进入首页，展示全部学员的出勤率
     *
     * @return
     */
    @RequestMapping("listStudentInfo")
    @ResponseBody
    public Map<String, Object> listStudentInfo(@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, String examplesMap) {
        Map<String, Object> map = new HashMap<>();

        try {
            Map<String, Object> result = attendanceService.listAttendance(pageNum, pageSize, examplesMap);
            map.put("result", true);
            map.put("pageInfo", result.get("pageInfo"));
            map.put("userList", result.get("userList"));
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result", false);
        } finally {
            return map;
        }
    }

    /**
     * 教学主管进入查看所有学员考勤信息页面
     *
     * @return
     */
    @RequestMapping("teachermanagerIndex")
    public String teachermanagerIndex() {

        return "WEB-INF/attendance/teachermanagerIndex";
    }

    /**
     * 教学主管进入查看出勤率页面
     *
     * @return
     */
    @RequestMapping("teacnhermanagerAttendance")
    public String teacnhermanagerAttendance() {
        return "WEB-INF/attendance/teacnhermanagerAttendance";
    }

    /**
     * 教学主管查询班主任所带班级的考勤率
     */
    @RequestMapping("listAttendanceByCmid")
    @ResponseBody
    public Map<String, Object> listAttendanceByCmid(Integer pageNum, Integer pageSzie, String examplesMap) {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> result = new HashMap<>();
        try {

            result = attendanceService.listAttendanceByClassManagerid(pageNum, pageSzie, examplesMap);
            List<User> classManagers = attendanceService.getClassManager();


            map.put("classmanager", result.get("classmanager"));
            map.put("attendance", result.get("resultMap"));
            map.put("classManagers", classManagers);
            map.put("result", true);

        } catch (Exception e) {
            System.out.println("12345");
            e.printStackTrace();
            map.put("result", false);
            map.put("msg", e.getMessage());
        } finally {
            return map;
        }
    }
}
